package work.chief.dao.impl;

import work.chief.dao.TrolleyDao;
import work.chief.entity.Trolley;
import work.chief.utils.DBUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class TrolleyDaoImpl implements TrolleyDao {
    //查询是否存在同一商品
    @Override
    public Trolley selectByTidAndUid(int uid, int pid) {
        Trolley trolley =null;
        try {
            QueryRunner qr = DBUtils.getInstance().getQrDb();
            //orders_number 有记录不为空，表示已经购买
            String sql="select * from trolley where uid=? and pid=? and orders_number is null";
            Object [] objects={uid,pid};
            trolley=qr.query(sql,new BeanHandler<>(Trolley.class),objects);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return trolley;
    }

    //更新
    @Override
    public int updateByUidAndPid(int uid, int pid, int number) {
        //设置一个参数记录更新状态
        int num=0;
        try {
            QueryRunner qr = DBUtils.getInstance().getQrDb();
            String sql="update trolley set number=? where uid=? and pid=?";
            Object [] objects={number,uid,pid};
            num=qr.update(sql,objects);     //更新成功，num=1
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return num;
    }

    //插入
    @Override
    public int addTrolley(Trolley trolley) {
        int num =0;

        try {
            QueryRunner qr = DBUtils.getInstance().getQrDb();
            String sql = "insert into trolley(uid,pid,number)values(?,?,?)";
            Object [] objects={trolley.getUid(),trolley.getPid(),trolley.getNumber()};
            num=qr.update(sql,objects);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return num;
    }

    @Override
    public Integer selectTrolleyCount(int uid) {
        QueryRunner qr = DBUtils.getInstance().getQrDb();
        try {
            String sql="select count(1) from trolley where uid=? and orders_number is null";
            Long count = (Long)qr.query(sql, new ScalarHandler(), uid);
            if (count!=null){
                //将long转换成Integer，可以先转换成String
                return Integer.parseInt(count+"");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Trolley> selectAllTrolley(int uid) {
        List<Trolley> trolleyList=null;
        try {
            QueryRunner qr = DBUtils.getInstance().getQrDb();
            String sql="select * from trolley where uid =? and orders_number is null";
            trolleyList = qr.query(sql, new BeanListHandler<Trolley>(Trolley.class), uid);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return trolleyList;
    }

    @Override
    public int deleteByTid(int tid) {
        int num =0 ;
        try {
            QueryRunner qr = DBUtils.getInstance().getQrDb();
            String sql="delete from trolley where tid =?";
            num = qr.update(sql, tid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return num;
    }

    @Override
    public int deleteByTids(String s) {
        int num=0;
        try {
            QueryRunner qr = DBUtils.getInstance().getQrDb();
            String sql="delete from trolley where tid in ("+s+")";
            num = qr.update(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return num;
    }

    @Override
    public Trolley selectByTid(int tid) {
        Trolley trolley =null;
        try {
            QueryRunner qr = DBUtils.getInstance().getQrDb();
            String sql="select * from trolley where tid=?";
            trolley = qr.query(sql, new BeanHandler<>(Trolley.class), tid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return trolley;
    }

    /**
     * 生成订单时修改购物车
     * @param tid
     * @param orders_name
     * @return
     */
    @Override
    public int updateTrolley(int tid, String orders_name) {
        int num=0;
        try {
            String sql="update trolley set orders_number=? where tid=?";
            Object[] objects={orders_name,tid};
            QueryRunner qr = DBUtils.getInstance().getQrDb();
            num=qr.update(sql,objects);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return num;
    }

    @Override
    public List<Trolley> selectByOrders_number(String orders_number) {
        List<Trolley> trolleyList = null;
        try {
            QueryRunner qr = DBUtils.getInstance().getQrDb();
            String sql="select * from trolley where orders_number=?";
            trolleyList = qr.query(sql, new BeanListHandler<Trolley>(Trolley.class), orders_number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return trolleyList;
    }
}
